//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        indicatorDots: false,
        autoplay: true,
        interval: 5000,
        duration: 500,
        
        banner: [],
        notice: [],
        association: [],
        school: [],
        old_course: [],
        hot_course: [],

        school_modal: '',
        select_school_id: '',
        select_school_name: '',

        role_modal: '',
        select_role: '',

        school_id: '',
        school_name: '',
    },

    onLoad: function (options) {
        //先授权登录，登录的用户信息里找到学校，没找到学校的再显示学校选择弹窗去更新学校
        this.getUserInfo();
    },

    onLogin: function (options) {
        this.getUserInfo();
    },

    onShow: function(){
        
    },

    //请先登录
    getUserInfo: function () {
        let _this = this;
        app.beLogin(function () {
            app._get('/api/login/user/getUserInfo', {
            }, function (response) {
                let data = response.data.data;
                if (response.data.code == 200) {
                    _this.setData({
                        user: data,
                    })
                    app.globalData.user = data;
                    //如果登录后没有绑定学校
                    if(data.school_id==null||data.school_name==null)
                    {
                        _this.showSchool();
                    }
                    //如果登录后没有绑定角色
                    else if(data.role==null)
                    {
                        _this.showRole();
                    }
                    else{
                        console.log(2)
                        _this.setData({
                            school_id: data.school_id,
                            school_name: data.school_name
                        })
                        _this.getBanner();
                        _this.getNotice();
                        _this.getAssociation();
                        _this.getOldCourse();
                        _this.getHotCourse();
                    }
                }
            })
        });
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh: function () {
        wx.showLoading({
            title: '正在刷新',
        })
        this.renderList(function () {
            setTimeout(function () {
                wx.stopPullDownRefresh();
                wx.hideLoading()
            }, 500)
        })
    },

    //重新渲染首页
    renderList: function (cb) {
        let setdata = {};
        setdata['banner'] = [];
        setdata['notice'] = [];
        setdata['old_course'] = [];
        setdata['hot_course'] = [];
        this.setData(setdata)
        this.getBanner();
        this.getNotice();
        this.getAssociation();
        this.getOldCourse();
        this.getHotCourse(cb);
    },

     //获取往期课程/教学风采
     getOldCourse: function (cb) {
        let _this = this;
        app._post('/api/login/course/getOldCourse', null, function (response) {
            let data = response.data.data;
            let setdata = {};
            setdata['old_course'] = data;
            _this.setData(setdata);
            console.log(_this.data.old_course)
        })
        typeof cb == 'function' && cb();
    },

    //获取热门课程
    getHotCourse: function (cb) {
        let _this = this;
        app._post('/api/login/course/getHotCourse', null, function (response) {
            let data = response.data.data;
            let setdata = {};
            setdata['hot_course'] = data;
            _this.setData(setdata);
            console.log(_this.data.hot_course)
        })
        typeof cb == 'function' && cb();
    },

    //获取学校列表
    getSchool: function () {
        let _this = this;
        app._get('/api/school/getSchool', null, function (response) {
            _this.setData({
                school: response.data.data
            })
        })
    },
    
    //获取轮播图
    getBanner: function () {
        let _this = this;
        app._get('/api/login/school/getBanner', null , function (response) {
            _this.setData({
                banner: response.data.data
            })
        })
    },

    //获取通知公告
    getNotice: function () {
        let _this = this;
        app._post('/api/login/school/getNotice', null , function (response) {
            _this.setData({
                notice: response.data.data
            })
        })
    },

    //获取社团文化
    getAssociation: function () {
        let _this = this;
        app._post('/api/login/school/getAssociation', null , function (response) {
            _this.setData({
                association: response.data.data
            })
        })
    },

    //去搜索
    go_search: function () {
        wx.switchTab({
            url: '/pages/course/course',
          })
    },

    /**
     * 去课程详情
     */
    toDetail: function(e){
        wx.navigateTo({
          url: '/pages/course_detail/course_detail?course_id='+e.currentTarget.dataset.course_id,
        })
    },

    /**
     * 去教学风采详情
     */
    toCourseStateDetail: function(e){
        wx.navigateTo({
          url: '/pages/course_state_detail/course_state_detail?course_id='+e.currentTarget.dataset.course_id,
        })
    },

    /**
     * 去更多教学风采
     */
    toCourseState: function(e){
        wx.navigateTo({
          url: '/pages/course_state/course_state',
        })
    },

     /**
     * 去社团文化详情
     */
    toAssociationDetail: function(e){
        if(e.currentTarget.dataset.association_id!=undefined){
            wx.navigateTo({
                url: '/pages/association_detail/association_detail?association_id='+e.currentTarget.dataset.association_id,
              })
        }
    },

    /**
     * 去更多社团文化
     */
    toAssociation: function(e){
        wx.navigateTo({
          url: '/pages/association/association',
        })
    },

    
    //显示弹窗
    showSchool: function () {
        this.getSchool();
        this.setData({
            school_modal: 'show'
        })
    },

    //选择学校
    selectSchool(e){
        this.setData({
            select_school_id: e.currentTarget.dataset.school_id,
            select_school_name: e.currentTarget.dataset.school_name
        })
    },

    //确定学校后
    confirmSchool: function () {
        let _this = this;
        app._post('/api/login/user/updateSchool', {
            school_id: _this.data.select_school_id,
            school_name: _this.data.select_school_name
        }, function (response) {
            if (response.data.code == 200) {
                _this.setData({
                    school_modal: '',
                    school_id: _this.data.select_school_id,
                    school_name: _this.data.select_school_name,
                })
                app.globalData.school_id=_this.data.select_school_id;
                app.globalData.school_name=_this.data.select_school_name;
                _this.getUserInfo();
            }
        })
    },

    //显示弹窗
    showRole: function () {
        this.getSchool();
        this.setData({
            role_modal: 'show'
        }) 
    },

    //选择学生
    checkStudent(){
        this.setData({
            select_role: 1
        })
    },

    //选择教师
    checkTeacher(){
        this.setData({
            select_role: 2
        })
    },

     //选择班主任
     checkClassmaster(){
        this.setData({
            select_role: 5
        })
    },
    
     //选择校长
     checkSchoolmaster(){
        this.setData({
            select_role: 6
        })
    },
    
    
    //确定角色后
    confirmRole(){
        this.setData({
            role: this.data.select_role
        })

        let _this = this;
        app._post('/api/login/user/updateRole', {
            role: _this.data.select_role
        }, function (response) {
            console.log(response)
            if (response.data.code == 200) {
                _this.setData({
                    role_modal: '',
                    role: _this.data.select_role
                })
                app.globalData.role=_this.data.select_role
                _this.getUserInfo();
            }
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: "课后延时小程序",
        }
    },


})